#include "view_distance_estimate.h"
#include "view_distance_estimate.inc"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_ctrl.h"
#include "display_interface.h"
#include "datetime.h"
#include "heart_rate_interface.h"
//#include "font/num_font.h"
#include "font_mngr.h"
//#include "view_common.h"
#include "app_trace.h"
#include "app_timer.h"
#include "times/view_time_0.h"
//  #define RSSI_SHOW
#ifdef RSSI_SHOW
#warning debug
#endif
#include "distance_estimate.h"
#define LOG(fmt, ...) // app_trace_log(fmt "\r\n", ##__VA_ARGS__)

void view_distance_estimate_base_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
#ifdef RSSI_SHOW
        disp_ctrl.auto_close_delay = 5000;
        memcpy(&disp_ctrl.com_data.i_temp[21], &ble_devices, sizeof(ble_devices));
#endif
        disp_ctrl.auto_refresh_time = 1000;
        disp_ctrl.com_data.i_temp[20] = m_sys_cfg_data.warning_distance;
    }

    uint8_t const *icon = 0;
    uint8_t const *distance_icon = 0;
    switch (disp_ctrl.com_data.i_temp[20] / 10)
    {
    case 1:
        distance_icon = icon_walnutin;
        icon = icon_1m;
        break;
    case 2:
        distance_icon = icon_walnutin;
        icon = icon_2m;
        break;
    default:
        distance_icon = icon_close;
        disp_ctrl.com_data.i_temp[20] = 0;
        set_warning_distance(disp_ctrl.com_data.i_temp[20]);
        icon = icon_off;
        break;
    }
#ifdef RSSI_SHOW
    char buffer[128] = {0};
    char *under_line = 0;
    Ble_device *ble_devices = (Ble_device *)(&disp_ctrl.com_data.i_temp[21]);
    for (int i = 0; i < 4; i++)
    {
        if (ble_devices[i].rssi < 0)
        {
            under_line = strchr((char const *)ble_devices[i].name, '_');
            // under_line = (char const *)ble_devices[i].name;
            if (under_line == 0)
            {
                break;
            }
            sprintf(buffer, "%s", under_line + 1);
            disp_show_font_string_utf8_mid(i * 32, buffer, 0);
            sprintf(buffer, "%d->%d", ble_devices[i].rssi, ble_devices[i].distance);
            disp_show_font_string_utf8_mid(i * 32 + 16, buffer, 0);
        }
        else
        {
            break;
        }
    }
#else
    if (distance_icon)
    {
        draw_on_chip_image(4, 14, distance_icon);
    }
    // _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 48, 17, distance_index);
#endif
    if (icon)
    {
        draw_on_chip_image(27, 125, icon);
    }
}

view_event_result_t view_distance_estimate_base_event(event_param_t const *param)
{

    switch (param->event)
    {
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
        disp_ctrl.com_data.i_temp[20] += 10;
        if (disp_ctrl.com_data.i_temp[20] > 20)
        {
            disp_ctrl.com_data.i_temp[20] = 0;
        }
        set_warning_distance(disp_ctrl.com_data.i_temp[20] * 10);
        display_set_update_request(view_distance_estimate, 0);
        return view_event_result_handled;

#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
        disp_ctrl.com_data.i_temp[20] -= 10;
        if (disp_ctrl.com_data.i_temp[20] < 0)
        {
            disp_ctrl.com_data.i_temp[20] = 20;
        }
        set_warning_distance(disp_ctrl.com_data.i_temp[20] * 10);
        display_set_update_request(view_distance_estimate, 0);
        return view_event_result_handled;
#endif
    }
    return view_event_result_not_handled;
}

void view_distance_estimate_display(int view)
{
    Expects(view >= view_distance_estimate);
    Expects(view < view_distance_estimate + VIEW_LEVEL_0_SUB_COUNT);
    switch (view)
    {
    case view_distance_estimate:
        view_distance_estimate_base_display();
        break;
    }
}
view_event_result_t view_distance_estimate_event(int view, event_param_t const *param)
{
    Expects(view >= view_distance_estimate);
    Expects(view < view_distance_estimate + VIEW_LEVEL_0_SUB_COUNT);
    view_event_result_t result = view_event_result_not_handled;
    switch (view)
    {
    case view_distance_estimate:
        result = view_distance_estimate_base_event(param);
        break;
    }
    if (result == view_event_result_handled)
    {
        return result;
    }
    return view_event_result_not_handled;
}
